//Component Object
Component({
  options: {
    pureDataPattern: /^_/ // 指定所有 _ 开头的数据字段为纯数据字段
  },
  data: {
    curSwiperIndex:0,
    _lastSwiperIndex:0,
    _lastAnimationSwiperIndex:0,
    isVideoPlaying:false,
    _videoContextList:[],
    activeVideoList:[
      {
        "mediaId" : 71,
        "mediaTitle" : "老年人高血压的并发症",
        "videoUrl" : "https://static.wanhuhealth.com/media/e1797379-a6ef-42e6-b151-1f8a92ef244b.mp4",
        "mediaFactUrl" : null,
        "mediaAccountId" : 159,
        "favCount" : 2,
        "clickCount" : 0,
        "shareCount" : 6,
        "isFav" : 0,
        "isClick" : 0,
        "isShare" : 0,
        "dialogueContent" : {
          "dialogId" : 2,
          "dialogTitle" : "标题2",
          "dialogVoice" : "地址2",
          "questionnaireId" : 2
        }
      },
      {
        "mediaId" : 71,
        "mediaTitle" : "老年人高血压的并发症",
        "videoUrl" : "https://static.wanhuhealth.com/media/e1797379-a6ef-42e6-b151-1f8a92ef244b.mp4",
        "mediaFactUrl" : null,
        "mediaAccountId" : 159,
        "favCount" : 2,
        "clickCount" : 0,
        "shareCount" : 6,
        "isFav" : 0,
        "isClick" : 0,
        "isShare" : 0,
        "dialogueContent" : {
          "dialogId" : 2,
          "dialogTitle" : "标题2",
          "dialogVoice" : "地址2",
          "questionnaireId" : 2
        }
      }
    ]
  },
  methods: {
    swiperAnimationfinish: function (e) {
      // console.log('swiperAnimationfinish e:',e)
      let curSwiperIndex = e.detail.current
      let _lastAnimationSwiperIndex = this.data._lastAnimationSwiperIndex
      //  用户没有滑动成功，直接return，不执行
      if (_lastAnimationSwiperIndex === curSwiperIndex) {
        return
      }
      this.data._videoContextList.forEach((videoContext, index) => {
        if (index === curSwiperIndex) {
          videoContext.play()
        } else {
          videoContext.pause()
        }
      })
      this.setData({
        curSwiperIndex: curSwiperIndex,
        _lastAnimationSwiperIndex: curSwiperIndex
      })
    },
    videoPlay() {
      // play和pause是异步的，有时候pause先触发，有时候play先触发，所以需要延时设置isVideoPlaying
      this.setData({
        isVideoPlaying: true
      })
      
    },
    videoTap() {
      let videoContext = this.data._videoContextList[this.data.curSwiperIndex]
      if (videoContext.paused) {
        videoContext.play()
        this.setData({
          isVideoPlaying: true,
        })
       
      } else {
        videoContext.pause()
        this.setData({
          isVideoPlaying: false
        })
      }
    },

    continuePlaying() {
      console.log('continuePlaying')
      this.data._videoContextList[this.data.curSwiperIndex].play()
      this.setData({
        isVideoPlaying: true
      })
    },
  },
 
  attached: function(){
    let busMedia = wx.getStorageSync('busMedia') || this.data.activeVideoList;
    this.setData({
      activeVideoList: busMedia
    },()=>{
      this.setData({
        _videoContextList: this.data.activeVideoList.map((item, index) => {
          return wx.createVideoContext('video' + index, this)
        })
      },()=>{
        this.data._videoContextList[0].play()
      })
    })

  },

  detached: function(){

  },
});